package com.china08.yxyapi.api.comm.upload;

import java.io.IOException;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.util.ThumbnailsUtil;
import com.china08.yxyapi.util.YXCSPUtil;

@CrossOrigin
@RestController("UploadFile2Controller")
public class UploadFile2Controller {
    @Autowired
    private YXCSPUtil yxcspUtil;

    /**
     * 上传文件
     * 
     * @throws IOException
     */
    @RequestMapping(value = "/api_yw/comm/uploadFile2", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel upLoadWenDoc(//
            @RequestParam(value = "file", required = false) MultipartFile file) throws IOException {

        RespModel respModel = new RespModel();
        try {

            Assert.isTrue((file != null), "为选择文件");

            String name = file.getOriginalFilename();
            Assert.isTrue(StringUtils.isNotBlank(name), "文件没有名称");

            // 文件后缀
            String filePostfix = FilenameUtils.getExtension(name);
            String fileType = "doc,docx,ppt,pptx,xls,xlsx,pdf,txt,zip,rar,7z,png,bmp,jpg,gif,jpeg,JPEG,JPG";
            Assert.isTrue(fileType.contains(filePostfix), "文件类型错误");

            String md5 = DigestUtils.md5Hex(file.getBytes());
            long size = file.getSize();
            String title = name.substring(0, name.indexOf("."));

            //
            boolean b1 = StringUtils.contains(filePostfix, "png");
            boolean b2 = StringUtils.contains(filePostfix, "jpg");
            // boolean b3 = StringUtils.contains(filePostfix, "gif");
            boolean b4 = StringUtils.contains(filePostfix, "jpeg");
            boolean b6 = StringUtils.contains(filePostfix, "JPG");
            boolean b7 = StringUtils.contains(filePostfix, "JPEG");

            byte[] bytes = file.getBytes();
            // 是图片
            if (b1 || b2 || b4 || b6 || b7) {
                bytes = ThumbnailsUtil.perfectForWidth(file.getBytes(), 640, filePostfix);
                MockMultipartFile newFile = new MockMultipartFile(file.getName(), file.getOriginalFilename(),
                        file.getContentType(), bytes);
                file = newFile;
            }

            String url = yxcspUtil.push(file);
            respModel.name = name;
            respModel.md5 = md5;
            respModel.size = size;
            respModel.title = title;
            respModel.ext = filePostfix;
            respModel.url = url;
            respModel.setMessage("上传成功");
        } catch (Exception e) {
            respModel.success = false;
            respModel.msg = e.getMessage();
        }
        return respModel;
    }

    protected static class RespModel extends YxyApiRespModel {

        // 文件名称
        public String name;
        // 文件md5
        public String md5;
        // 文件 大小
        public long size;
        // 文件标题
        public String title;
        // 文件后缀
        public String ext;
        // 文件url
        public String url;
        public boolean success = true;
        public String msg;
    }
}
